package br.com.educa.util;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

/**
 *
 * @author user
 */
public class Mailmanx {
	
	public static void main(String args[]) throws Exception{
		Mailmanx mailmanx  = new Mailmanx();
		mailmanx.Sendmail("teste", "teste.");
	}

	//	new MailWithPasswordAuthentication().run();

	String body,subject;
	@SuppressWarnings("rawtypes")
	public void mail_alist(ArrayList lines,String asubject)
	{
		//String user, pass, subject, body;

		subject="Alert :\t"+asubject;
		body="START of message\n";

		for(int i=0; i<lines.size(); i++) {
			body+=((String)lines.get(i))+"\n";
		}
		try {
			Sendmail(body, subject);
		} catch (MessagingException ex) {
			Logger.getLogger(Mailmanx.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public void Sendmail(String body, String subject) throws MessagingException ////this is used to send the emails
	{

		Session session  = getSession();
		Message message = new MimeMessage(getSession());

		////test bit
		// message.addRecipient(RecipientType.TO, new InternetAddress("TO_mail@corporate.com"));///change to dist list
		//	message.addFrom(new InternetAddress[] { new InternetAddress("from_mail@corporate.com") });
		///end test bit

		message.addRecipient(RecipientType.TO, new InternetAddress("filipe.marinho.brito@gmail.com"));///change to dist list
		message.addFrom(new InternetAddress[] { new InternetAddress("filipe.brito@mj.gov.br") });

		message.setSubject(subject);
		message.setContent(body, "text/plain");

		Transport transport = session.getTransport("smtp");
		transport.connect();
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
		
	}

	private Session getSession() {
		Authenticator authenticator = new Authenticator();

		Properties properties = new Properties();
		properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.starttls.enable","true");
		properties.setProperty("mail.smtp.host", "smtpsrv.mj.gov.br");
		properties.setProperty("mail.smtp.port", "25");

		return Session.getInstance(properties, authenticator);
	}

	private class Authenticator extends javax.mail.Authenticator {
		private PasswordAuthentication authentication;

		public Authenticator() {
			String username = "filipe.brito@mj.gov.br";
//			String username = "corp-domain\\user";
			String password = "m9s2h3u7O";
			authentication = new PasswordAuthentication(username, password);
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return authentication;
		}
	}
	
}